mui.init({
	beforeback: function() {
		var parentObj = plus.webview.currentWebview().opener();
		mui.fire(parentObj, 'dataList');
	}
});
var userInfo;
var index_goods;
var flag;

mui.ready(function() {
	mui('.mui-scroll-wrapper').scroll({
		bounce: true,
		indicators: true,
		deceleration: mui.os.ios ? 0.003 : 0.0009
	});
	mui('.mui-scroll').on('tap', '.mui-control-item:not(.mui-active)', function() {
		mui('.mui-slider').slider().gotoItem(this.getAttribute('tab-id'));
	});
});

mui.plusReady(function() {
	hideScrollBar();
	var self = plus.webview.currentWebview();
	index_goods = new Vue({
		el: '#product_detail',
		data: {
			num: '',
			buy_num: 1,
			flag: '',
			title: '',
			image: '',
			sku: [],
			tab_sku: '',
			tab_price: '',
			sku_id: '',
			shipping_price: '',
			tab_id: '1',
			goods_detail: [],
			product_detail: [],
			banner_img: [],
			collection_status: '',
			address: '',
			loginSta: ''
		},
		mounted() {
			this.get_sDetail();
			this.getAddress();
			this.updateStatus();
		},
		updated: function() {
			this.$nextTick(function() {
				new Swiper('#swiper', {
					loop: false,
					autoplay: false,
					speed: 300,
					pagination: {
						el: '.swiper-pagination',
						type: 'fraction',
					},
				});
			})
		},
		computed: {
			total: function() {
				return Math.floor(this.buy_num * this.tab_price);
			}
		},
		methods: {
			updateStatus: function() {
				showWatting();
				var api_host = plus.storage.getItem('HOST_URL');
				var self = this;
				mui.ajax({
					url: api_host + '/api/linxuan/user/checkLoginStatus',
					dataType: 'json',
					headers: {
						'User-Token': plus.storage.getItem('UserToken')
					},
					timeout: 3000,
					type: 'get',
					success: function(msg) {
						if (msg.code == 1) {
							self.loginSta = 1;
							self.user_detail = msg.data;
						} else {
							self.loginSta = 0;
						}
					},
					error: function(xhr, type, errorThrown) {
						mui.toast('登录状态请求失败请重试', {
							duration: '300'
						});
					},
					complete: function() {
						plus.nativeUI.closeWaiting();
					}
				});
			},

			get_sDetail: function() {
				get_product_content(self.product_id);
			},
			qq: function() {
				customerService();
			},

			share: function() {
				$('.popMask').show();
				$('.popShare').slideDown(200);
			},
			cancle_share: function() {
				$('.popMask').hide();
				$('.popShare').slideUp(200);
			},
			favorite: function(goodsid, name) {
				updateProductCollection('shop', goodsid, name.title, name, index_goods);
			},
			buy: function() {
				if (index_goods.loginSta == 0) {
					mui.toast('您还没有登录，正在前往登录页面...', {
						duration: '800',
						type: 'div'
					});
					setTimeout(function() {
						mui.openWindow({
							url: '../../logReg/login.html',
							createNew: false,
							id: 'loginHtml',
							show: {
								autoShow: true,
								anishow: 'pop-in',
								duration: 400
							},
							waiting: {
								autoShow: false,
							}
						});
					}, 1000)
				} else {
					$('.popMask').show();
					$('.popBuy').animate({
						'bottom': '0%'
					}, 300).show();
				}
			},
			hideMask: function() {
				$('.popMask').hide();
				$('.popBuy').animate({
					'bottom': '-100%'
				}, 300).hide(300);
			},
			share_layer: function() {
				$('.popMask').hide();
				$('.popBuy').animate({
					'bottom': '-100%'
				}, 300).hide(300);
			},
			fix_buy_btn: function(title, buy_num, tab_price, shipping_price, sku_id, image) {
				$('.popMask').hide();
				$('.popBuy').animate({
					'bottom': '-100%'
				}, 300).hide(300);
				mui.openWindow({
					url: 'mallOrder.html',
					extras: {
						'title': title,
						'tab_sku': index_goods.tab_sku,
						'buy_num': buy_num,
						'tab_price': tab_price,
						'shipping_price': shipping_price,
						'sku_id': sku_id,
						'image': image,
						'product_id': self.product_id,
						'address': index_goods.address
					},
					show: {
						autoShow: true,
						anishow: 'pop-in',
						duration: 400
					},
					waiting: {
						autoShow: false,
					}
				})
			},
			buyInfo: function() {
				$(this).addClass('active').siblings('a').removeClass('active');
			},
			increase: function() {
				this.buy_num = parseInt(index_goods.buy_num) + 1;
			},
			decrease: function() {
				if (index_goods.buy_num < 2) {
					mui.toast("购买数量必须大于0", {
						duration: '2000',
						type: 'div'
					});
					return;
				}
				this.buy_num = parseInt(index_goods.buy_num) - 1;

			},
			select_sku: function(e) {
				this.sku_id = e.target.getAttribute('tab-index');
				this.tab_sku = e.target.getAttribute('tab-sku');
				this.tab_price = e.target.getAttribute('tab-price');
			},
			select_tab: function(e) {
				this.tab_id = e.target.getAttribute('tab-id');
			},
			getAddress: function() {
				showWatting();
				var api_host = plus.storage.getItem('HOST_URL');
				mui.ajax({
					url: api_host + '/api/user/user/userAddress',
					dataType: 'json',
					type: 'get',
					headers: {
						'User-Token': plus.storage.getItem('UserToken')
					},
					timeout: 3000,
					success: function(msg) {
						if (msg.code == 1) {
							index_goods.address = msg.data.address;
						}
					},
					error: function(xhr, type, errorThrown) {
						console.log(errorThrown);
					},
					complete: function() {
						plus.nativeUI.closeWaiting();
					}
				})
			},
		}
	})
})


function get_product_content(pid) {
	showWatting();
	var api_host = plus.storage.getItem('HOST_URL');

	mui.ajax({
		url: api_host + '/api/shop/products/getProductsDetailsV116/id/' + pid,
		dataType: 'json',
		data: {
			'id': pid
		},
		headers: {
			'User-Token': plus.storage.getItem('UserToken')
		},
		timeout: 3000,
		type: 'get',
		success: function(msg) {
			if (msg.code == 1) {
				index_goods.goods_detail = msg.data;
				index_goods.banner_img = msg.data.product_detail.image;
				index_goods.sku = msg.data.sku;
				index_goods.tab_price = msg.data.sku[0].price;
				index_goods.sku_id = msg.data.sku[0].id;
				index_goods.tab_sku = msg.data.sku[0].specification;
				index_goods.product_detail = msg.data.product_detail;
				index_goods.customService = msg.data.customService;
				index_goods.flag = msg.data.product_detail.star;
				index_goods.title = msg.data.product_detail.title;
				index_goods.image = msg.data.product_detail.image;
				index_goods.shipping_price = msg.data.product_detail.shipping_price;
				index_goods.collection_status = msg.data.product_detail.star;
				mui(".mallCont").on('tap', 'img', function() {
					var src = $(this).attr('src');
					openBigPic([src]);
				});
				plus.nativeUI.closeWaiting();
				mui.currentWebview.show();
			} else {
				mui.toast(msg.msg);
			}
		},
		error: function(xhr, type, errorThrown) {
			console.log(errorThrown);
		},
		complete: function() {
			plus.nativeUI.closeWaiting();
		}
	});
}


//操作收藏
function add_collection(goodsid, name) {
	var api_host = plus.storage.getItem('HOST_URL');
	mui.ajax({
		url: api_host + '/api/other/other/add_collection',
		data: {
			'product_id': goodsid,
			'product_title': name.title,
			'collection_info': name,
			'product_type': 'shop'
		},
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		headers: {
			'User-Token': plus.storage.getItem('UserToken')
		},
		timeout: 3000,
		success: function(msg) {
			index_goods.collection_status = msg.data.collection_status;
			mui.toast(msg.msg, {
				duration: '1000'
			});
		},
		error: function(xhr, type, errorThrown) {
			mui.toast('系统错误，请联系客服！', {
				duration: '2000',
				type: 'div'
			});
		}
	});
}

/**
 * 分享按钮点击事件
 */
function shareHref() {
	plus.share.getServices(successCB, errorCB);
	var api_host = plus.storage.getItem('HOST_URL');

	var content = index_goods.product_detail.title;
	var id = index_goods.product_detail.id;

	var href = api_host + '/api/shop/products/getproductsdetails/id/' + id;
	var message = {
		title: "公选王", //应用名字
		content: content,
		href: href, //分享出去后，点击跳转地址
		thumbs: ["http://img3.duitang.com/uploads/item/201603/03/20160303215551_eNwPK.jpeg"] //分享缩略图
	}
	//调起分享
	plusShare(message, function(res) {
		//分享回调函数
		if (res) {
			plus.nativeUI.toast("分享成功");
		} else {
			plus.nativeUI.toast("分享失败");
		}
	})
}

function successCB(e) {
	console.log(JSON.stringify(e));
}

function errorCB(e) {
	console.log(JSON.stringify(e));
}

function dataList() {
	index_goods.get_sDetail();
}

window.addEventListener('dataList', function(event){
	var embed = plus.webview.currentWebview();
	embed.reload(true);
})



